﻿using PP.PaintLib;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace MyPaint
{
    public partial class FrmEatColor : Form
    {
        Form f = new Form();
        Timer timer = new Timer();
        public FrmEatColor()
        {
            f.FormBorderStyle = FormBorderStyle.None;//无边框
            f.Cursor = Cursors.Cross;
            f.WindowState = FormWindowState.Maximized;
            f.Opacity = 0.01;
            f.ShowInTaskbar = false;
            f.Text = "取色中...";
            f.KeyDown += F_KeyDown;

            InitializeComponent();

            this.StartPosition = FormStartPosition.CenterScreen;

            this.timer.Tick += Timer_Tick;
            //关闭
            this.btnClose.Click += (s, e) =>
            {
                timer.Stop(); 
                f.Close(); 
                this.Close(); 
                GC.Collect();
            };
            //拾取
            this.btnStartEat.Click += BtnStartEat_Click;
            this.txtHex.MouseEnter += (s, e) =>
            {
                this.txtHex.Focus();
                this.txtHex.SelectAll();
                this.txtHex.Copy();
            };
            //点击屏幕事件
            f.MouseDown += (s, e) => 
            {
                this.timer.Stop();
                f.Hide();
            };
            
            this.FormBorderStyle = FormBorderStyle.None;
            this.PicColor.Text = "";

            BtnStartEat_Click(null,null);
        }

        private void BtnStartEat_Click(object sender, EventArgs e)
        {
            f.TopMost = true;
            f.Show();
            this.timer.Start();
        }

        /// <summary>
        /// 键盘点击抬起事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void F_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.timer.Stop();
                f.Hide();
            }
            Point pCur = MousePosition;
            //方向键微调
            if (e.KeyCode == Keys.Up)
                PPWin32.SetCursorPos(pCur.X, pCur.Y - 1);
            if (e.KeyCode == Keys.Left)
                PPWin32.SetCursorPos(pCur.X - 1, pCur.Y);
            if (e.KeyCode == Keys.Right)
                PPWin32.SetCursorPos(pCur.X + 1, pCur.Y);
            if (e.KeyCode == Keys.Down)
                PPWin32.SetCursorPos(pCur.X, pCur.Y + 1);
        }

        /// <summary>
        /// 实时显示坐标+句柄+颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Tick(object sender, EventArgs e)
        {
            //鼠标的坐标
            this.MousePosTips.Text = "鼠标坐标：" + MousePosition.X + "," + MousePosition.Y;
            //显示句柄
            this.txtInprt.Text = "句柄：" + PPWin32.WindowFromPoint(MousePosition);
            //当前窗体自动跟随鼠标
            this.MoveForm();
            //利用API获取当前鼠标坐标点的像素色
            Color c = PPWin32.GetPixelColor(MousePosition);

            //显示在网页中显示的编码
            this.txtHex.Text = "#" + c.Name.ToUpper().Substring(2);
            //显示RGB三位色
            this.txtRGB.Text = c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString();
            //设置label的颜色
            this.PicColor.BackColor = c;
            //显示放大镜
            this.ShowPictureBox(MousePosition);

            //手动回收，避免内存暴涨
            GC.Collect();

            //有焦点才能用键盘：上下左右
            f.Focus();
        }

        /// <summary>
        /// 显示缩略图
        /// </summary>
        /// <param name="p"></param>
        private void ShowPictureBox(Point p)
        {
            //创建一个画布-展示鼠标周围
            using (Bitmap bmp = new Bitmap(20, 20))
            {
                //在这块画布上建立一个绘图对象
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    //截取复制当前鼠标周围内容（0，0，20，20）
                    g.CopyFromScreen(p.X - 10, p.Y - 10, 0, 0, bmp.Size);
                    //放大图片（会失真，造成缩略效果）
                    Image pThumbnail = bmp.GetThumbnailImage(this.picBig.Width, this.picBig.Height, null, new IntPtr());
                    //画放大图
                    g.DrawImage(bmp, 10, 10, pThumbnail.Width, pThumbnail.Height);
                    this.picBig.Image = pThumbnail;

                    //画放大图里的小框框
                    using (Graphics g2 = Graphics.FromImage(this.picBig.Image))
                    {
                        g2.DrawRectangle(Pens.Black, this.picBig.Width / 2 - 5, this.picBig.Height / 2 - 5, 10, 10);
                    }        
                }
            }

        }

        /// <summary>
        /// 当前窗体自动跟随鼠标
        /// </summary>
        private void MoveForm()
        {
            Point p = new Point();
            p.X = MousePosition.X + 10;
            p.Y = MousePosition.Y + 10;

            Size s = Screen.PrimaryScreen.Bounds.Size;

            p.X -= p.X > s.Width - this.Width ? this.Width + 20 : 0;
            p.Y -= p.Y > s.Height - this.Height ? this.Height + 20 : 0;

            this.Location = p;
        }
    }
}
